package cn.edu.sdjzu.util;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author TG.SI@188.COM
 * @version 1.0
 * 19th Oct. 2020
 * 为SQL的where子句建模。
 * 本类可以将符合条件的json数组转换为where子句，以便用于SQL语句的拼接
 * 适合于字符型、数值型字段
 * 未测试日期型、布尔型字段
 */
public class Condition {
    /**
     * @param conditionList 多条件形成的List对象，元素为Condition类型
     * 示例[{"value":"一","key":"name","operator":"LIKE"},
     *{"value":"9","key":"department_id","operator":"="}]
     * 如果字段为字串的，无须设置模糊查询。
     * @return 多个条件对应的where子句。数值型字段也使用引号包围起来。
     * 本类依赖Fastjson
     * */
    public static String toWhereClause(List<Condition> conditionList){
        //以WHERE开头，注意前后的空格
        StringBuilder sb = new StringBuilder(" WHERE ");
        //将字段名、操作符和字段值依次拼接为字串
        //每个条件后面都附加上" AND "，注意前后的空格
        for(Condition c:conditionList){
            sb.append(c.getKey() ).append(" ").append(c.getOperator()).append(" ");
            String value = c.getValue();
            //如果操作符是like，自动设置为模糊匹配
            if("like".equalsIgnoreCase(c.getOperator())) {
                value = "%" + value + "%";
                //把value值用引号包括起来
                value = TxtHelper.getSingleQuoteMarked(value);
            }

            sb.append(value);
            sb.append(" AND ");
        }
        //把最后一个条件后面的“ and ”删除（删除最后5个字符）
        TxtHelper.truncEnding(sb, 5);
        return sb.toString();
    }

    //不能定义无参构造器，否则fastjson会使用它来创建对象。

    public Condition(String key, String operator, String value) {
        this.key = key;
        this.operator = operator;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getOperator() {
        return operator;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Condition{" +
                "key='" + key + '\'' +
                ", operator='" + operator + '\'' +
                ", value='" + value + '\'' +
                '}';
    }

    //字段名
    private String key;
    //操作符
    private String operator;
    //字段值
    private String value;

    public static void main(String[] args) {
        Condition condition1 = new Condition("name","like","一");
        Condition condition2 = new Condition("department_id","=","9");
        Set<Condition> conditionSet = new HashSet<Condition>();
        conditionSet.add(condition1);
        conditionSet.add(condition2);

        Object json = JSON.toJSON(conditionSet);
        System.out.println(json);

        //将请求body解析为JSONObject对象
        JSONArray conditionList_jsonArray = JSON.parseArray(json.toString());

        //解析为Java集合类对象
        List<Condition> conditionList = conditionList_jsonArray.toJavaList(Condition.class);
        List<Condition> conditionList1 = JSON.parseArray(json.toString(), Condition.class);
        System.out.println(Condition.toWhereClause(conditionList));

//        JSONArray conditionList_jsonArray =jsonObject.getJSONArray("conditionList");
        
    }
}
